import {p} from '../ajax.js'
    var div2 = document.getElementById("iot-div2");
    var img = document.createElement("img");
    var a = document.createElement("a");
    div2.appendChild(a);
    a.appendChild(img);
   // div2.appendChild(img);
    var divli = document.querySelectorAll(".iot-divli>div");
    img.style.aspectRatio = 16/7;
p("https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata").then((res)=>{
    //img.src = res.message[0].image_src;
     return res.message;
}).then((message)=>{
    // 轮播图实现代码
var i = 0;//图片
var index = 0;//小圆点
img.src = message[i].image_src;//初始化图片地址
divli[index].style.opacity = 0.8;//初始化小圆点


for(var j = 0;j<message.length;j++){
    let jj = j;
    document.getElementById("iot-divli"+j).addEventListener('click',change1);
    function change1(){
        img.src = message[jj].image_src;
        divli[jj].style.opacity = 0.8;
        for(var k = 0;k<3;k++){
            if(k!=jj){
                divli[k].style.opacity = 0.2;
            }
        }
        i = index = jj;//防止小圆点乱跳
    }
}


//点击第一个小圆点
// document.getElementById("iot-divli1").addEventListener('click',change1)
// function change1() {
//     index = i = 0;
//     img.src = message[i].image_src;
//     //a.href = "https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata" + message[i].navigator_url;
//     divli[index].style.opacity = 0.8;
//     divli[1].style.opacity = 0.2;
//     divli[2].style.opacity = 0.2;
// }
// //点击第二个小圆点
// document.getElementById("iot-divli2").addEventListener('click',change2)
// function change2() {
//     index = i = 1;
//     img.src = message[i].image_src;
//     divli[index].style.opacity = 0.8;
//     divli[0].style.opacity = 0.2;
//     divli[2].style.opacity = 0.2;
// }
// //点击第三个小圆点
// document.getElementById("iot-divli3").addEventListener('click',change3)
// function change3() {
//     index = i = 2;
//     img.src = message[i].image_src;
//     divli[index].style.opacity = 0.8;
//     divli[0].style.opacity = 0.2;
//     divli[1].style.opacity = 0.2;
// }
function lunbo() {
    i++;
    if(i!=index){divli[index].style.opacity = 0.2}
    index++;
    if(index==3){index=0};
    divli[index].style.opacity=0.8;
    if(i==3){i=0};
    img.src = message[i].image_src;
}
setInterval(lunbo,2000)//定时调用函数
})

//初始状态宽度设置
var winwidth = document.getElementById("iot-div1-1").offsetWidth;//获取无具体宽度的div的宽度
   img.width = winwidth;
//窗口变化宽度设置
let getwinwidth = function(){
    var winwidth = document.getElementById("iot-div1-1").offsetWidth;//获取无具体宽度的div的宽度
   img.width = winwidth;
}
window.addEventListener('resize',getwinwidth);//监听窗口大小变化